
//eventSource通知
// 新建一个eventSource对象, 如果和服务器端断开连接，会自动尝试重新连接
//从session拿uid
var uid = 0
$.ajax({
    url:'http://localhost:8080/posterior_system_war_exploded/getUid',
    type:'get',
    datatype:'json',
    async: false,
    success:function (_data) {
        var code = _data.code;
        if(code==1){
            uid = _data.uid
            console.log(uid)
        }
    }

})
//未读通知铃铛 id:unreadNotify
function unreadNotifyBell(row) {

    //统计未读通知数
    var num = 0;
    $.ajax({
        url:"http://localhost:8080/posterior_system_war_exploded/notify/unread/count",
        type:'get',
        dataType:'json',
        data:{uid:uid},
        success:function (_data) {
            num = _data.count;
            $('#unreadNotify').html(num);
        }
    })

}
unreadNotifyBell(null)

var eventSource = new EventSource("http://localhost:8080/posterior_system_war_exploded/se/conn/"+Math.random().toString(36).slice(-8));
//有通知就会接收
eventSource.addEventListener("system_notify", function(msg) {
    var data = JSON.parse(msg.data)
    if(data.code==1){
        $('#notifyContent').bootstrapTable('refresh')
        Lobibox.notify('success', { width:$(window).width()/5,
            delay: 2000,
            size: 'mini',
            title: '通知',
            position: 'bottom right',
            showClass: 'fadeInDown',
            hideClass: 'fadeUpDown',
            msg:'你有新的通知'
        });
        renderUnreadUl()
        unreadNotifyBell(null)
    }
})

var unreadVm = new Vue({
    el: '#notifyMenu',
    data: {
        notifyData:[]
    },
    methods: {
        fn: function (e) {
            // 当前元素
            var path = $(e.currentTarget).attr('value')
            var nid = $(e.currentTarget).attr('nid')
            seeNotifyMethod(path,nid)
            renderUnreadUl()

        }
    }

})
//查看通知方法
function seeNotifyMethod(value,nid) {
    console.log(nid)
    //改变已读状态(uid nid)
    $.ajax({
        url:"http://localhost:8080/posterior_system_war_exploded/notify/addReadNotitfy",
        type:'put',
        dataType:'json',
        data:{uid:uid,nid:nid},
        success:function (_data) {
            unreadNotifyBell(null)
        }
    })
    //打开新窗口
    var url = value
    window.open(url, '手机百度', 'height=500, width=500, top=20, left=500, toolbar=no, menubar=no, scrollbars=no, resizable=false, location=no, status=no'); //参数： url, 名称, 窗体样式
}
//渲染通知铃铛列表
function renderUnreadUl() {
    $.ajax({
        url:"http://localhost:8080/posterior_system_war_exploded//notify/unread",
        type:'get',
        dataType:'json',
        data:{uid:uid},
        success:function (_data) {
            //成功后渲染列表
            if(_data.code=="1"){
                //先清空
                unreadVm.notifyData = []
                for(var i = 0;i<_data.notify.length;i++){
                    unreadVm.notifyData.push(_data.notify[i])
                }
            }
            //失败的情况返回错误信息
        }
    })
}

renderUnreadUl()